package com.google.code.swingonfire.component.foldable;

import java.awt.Component;
import java.util.EventListener;

import com.google.code.swingonfire.builder.AbstractJComponentBuilder;

public class JFoldablePanelBuilder extends AbstractJComponentBuilder<JFoldablePanelBuilder, JFoldablePanel>
{

    private Component contentComponent;
    private Component triggerComponent;

    public JFoldablePanelBuilder()
    {
        super(JFoldablePanel.class);
    }

    public JFoldablePanelBuilder(Component contentComponent)
    {
        this();

        contentComponent(contentComponent);
    }

    public JFoldablePanelBuilder(Component contentComponent, Component triggerComponent)
    {
        this(contentComponent);

        triggerComponent(triggerComponent);
    }

    public JFoldablePanelBuilder contentComponent(Component contentComponent)
    {
        this.contentComponent = contentComponent;

        return this;
    }

    public JFoldablePanelBuilder triggerComponent(Component triggerComponent)
    {
        this.triggerComponent = triggerComponent;

        return this;
    }

    @Override
    protected void populate(JFoldablePanel component)
    {
        if (contentComponent != null)
        {
            component.setContentComponent(contentComponent);
        }

        if (triggerComponent != null)
        {
            if (triggerComponent instanceof AbstractJFoldableTrigger)
            {
                component.setTriggerComponent((AbstractJFoldableTrigger) triggerComponent);
            }
            else
            {
                component.setTriggerComponent(new DefaultJFoldableTrigger(triggerComponent));
            }
        }

        super.populate(component);
    }

    @Override
    protected void setListener(JFoldablePanel component, EventListener listener)
    {
        super.setListener(component, listener);

        if (listener instanceof FoldableListener)
        {
            component.addFoldableListener((FoldableListener) listener);
        }
    }

}
